<template>
  <div class="accountPanel">
    <el-form label-width="60px" size="large" :model="account" :rules="accountRules" ref="formRef">
      <el-form-item label="帐号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" type="password" :show-password="true" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import type { FormRules, ElForm } from 'element-plus'
import { reactive } from 'vue';
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import useLoginStore from '@/stores/login/login'
import type { IAccount } from '../tpyes';
//定义account 数据
const account = reactive<IAccount>({
  name: '',
  password: '',
});
//校验规则
const accountRules: FormRules = {
  name: [
    { required: true, message: '必须输入帐号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '必须输入密码', trigger: 'blur' }
  ]
}
const formRef = ref<InstanceType<typeof ElForm>>()

const loginStore = useLoginStore()
//执行账户的登录逻辑
function loginAction() {
  formRef.value?.validate((valid) => {
    if (valid) {
      //向服务器发送网络请求
      loginStore.loginAccountAction(account)
    } else {
      ElMessage.error('账号名或密码未填写')
    }
  });
}
//暴露给父组件
defineExpose(
  {
    loginAction
  }
)
</script>

<style lang="less" scoped>
.accountPanel {}
</style>
